package com.qdlc.p2p.biz.service.user.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.impl.UserLoginServiceImpl;
import com.qdlc.p2p.biz.service.user.ResetPasswordStepModel;
import com.qdlc.p2p.biz.service.user.ResetPasswordStrategyService;
import com.qdlc.p2p.biz.service.user.UserPasswordService;
import com.qdlc.p2p.biz.util.NoticeTemplateSender;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.common.util.code.MD5;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.User;

@Service
public class UserPasswordServiceImpl implements UserPasswordService {

	@Resource
	UserDao userDao;
	
	@Autowired
	@Qualifier("resetPasswordEmailStrategyImpl")
	ResetPasswordStrategyService resetPasswordEmailStrategyImpl;
	
	@Autowired
	@Qualifier("resetPasswordMobilePhoneStrategyImpl")
	ResetPasswordStrategyService resetPasswordMobilePhoneStrategyImpl;
	
	/**
	 * [修改密码] 修改用户密码
	 * @param user 操作用户
	 * @param oldPassword 原密码
	 * @param newPassword 新密码
	 * @param confirmNewPassword 确认新密码
	 */
	@Override
	public ReturnResult modifyPassword(User user, String oldPassword, String newPassword, String confirmNewPassword) {
		
		//检测用户密码参数
		ReturnResult result = checkModifyPassword(user, oldPassword, newPassword, confirmNewPassword);
		if(result.isError()){
			return result;
		}
		
		//修改用户密码
		String newPasswordMD5 = MD5.encode(newPassword);
		user.setPwd(newPasswordMD5);
		userDao.update(user);
		
		//发送邮件通知
		sendSmsForModifyPassword(user);
		
		return result;
	}
	
	/**
	 * 检测修改密码请求参数
	 * @param user
	 * @param oldPassword
	 * @param newPassword
	 * @param confirmNewPassword
	 * @return
	 */
	private ReturnResult checkModifyPassword(User user, String oldPassword, String newPassword, String confirmNewPassword){
		
		if (StringUtil.isBlank(oldPassword)) {
			return new ReturnResult("请输入您的原密码!");
		}
		
		if(StringUtils.isBlank(newPassword)){
			return new ReturnResult("新密码不能为空!");
		}
		
		if(StringUtils.isBlank(confirmNewPassword)){
			return new ReturnResult("确认新密码不能为空!");
		}
		
		if (!newPassword.equals(confirmNewPassword)) {
			return new ReturnResult("新密码和确认密码不相同！");
		}
		
		if (newPassword.equals(oldPassword)) {
			return new ReturnResult("新密码不能和原密码相同！");
		}
		
		// 校验密码长度及组成
		String checkPwd = ValidateUtil.isPwd(newPassword);
		if (StringUtil.isNotBlank(checkPwd)) {
			return new ReturnResult(checkPwd);
		}

		String newPasswordMD5 = MD5.encode(newPassword);
		if (newPasswordMD5.equals(user.getPayPwd())) {
			return new ReturnResult("新密码不能和您的交易密码相同!");
		}
		
		//导入密码
		String passwordPar = oldPassword + UserLoginServiceImpl.PASSWORD_PAR; 
		String passwordMD5 = MD5.encode(oldPassword);
		String passwordParMD5 = MD5.encode(passwordPar);
		String userPassword = user.getPwd();
		if (!(passwordMD5.equals(userPassword) || passwordParMD5.equals(userPassword))) {
			return new ReturnResult("原密码错误!");
		}

		return ReturnResult.SUCCESS;
	}
	
	/**
	 * 发送短信、邮件、站内信通知
	 * @param user
	 */
	private void sendSmsForModifyPassword(User user){
		NoticeTemplateSender.newInstance(user, NoticeTplNid.PASSWORD_UPDATE).send();
	}
	
	
	/**
	 * 密码找回功能（手机找回、邮箱找回）
	 * 委托给ResetPasswordStrategyService策略类去处理
	 * @param stepModel
	 * @return
	 */
	@Override
	public ReturnResult resetPasswordByMobilePhone(ResetPasswordStepModel stepModel){
		int strategyType = stepModel.getStrategyType();
		ReturnResult result = ReturnResult.SUCCESS;
		if(strategyType == ResetPasswordStepModel.RESET_PASSWORD_STRATEGY_TYPE_PHONE){
			result = resetPasswordMobilePhoneStrategyImpl.resetPasswprd(stepModel);
		}else if(strategyType == ResetPasswordStepModel.RESET_PASSWORD_STRATEGY_TYPE_EMAIL){
			result = resetPasswordEmailStrategyImpl.resetPasswprd(stepModel);
		}
		return result;
	}
	
	
}
